//
//  ShipinCell.m
//  网易新闻
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "ZGL24VideoCell.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "ZGL24VideoModel.h"
@interface ZGL24VideoCell()<UIWebViewDelegate>
@property(nonatomic, retain)UIView *background;
@property(nonatomic, retain)UILabel *title;
@property(nonatomic, retain)UILabel *desc;
@property(nonatomic, retain)UILabel *details;
@property(nonatomic, retain)UIWebView *player;
@property(nonatomic, retain)UIButton *img;
@end
@implementation ZGL24VideoCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _background = [[UIView alloc]init];
        _background.layer.shadowColor = [UIColor blackColor].CGColor;
        _background.layer.shadowOffset = CGSizeMake(2,2);
        _background.layer.shadowOpacity = 0.8;
        _background.layer.shadowRadius = 4;
        [self.contentView addSubview:_background];
        
        _title = [[UILabel alloc]init];
        [_background addSubview:_title];
        
        _desc = [[UILabel alloc]init];
        _desc.textColor = [UIColor lightGrayColor];
        _desc.font = [UIFont systemFontOfSize:14];
        [_background addSubview:_desc];
        
        _details = [[UILabel alloc]init];
        _details.textColor = [UIColor lightGrayColor];
        _details.font = [UIFont systemFontOfSize:14];
        [_background addSubview:_details];

        _player = [[UIWebView alloc] init];
        _player.backgroundColor = [UIColor lightGrayColor];
        [(UIScrollView *)[[_player subviews] objectAtIndex:0] setBounces:NO];
        _player.allowsInlineMediaPlayback = YES;
        _player.mediaPlaybackRequiresUserAction = NO;
        _player.delegate = self;
        [_background addSubview:_player];
        
        _img = [UIButton buttonWithType:UIButtonTypeCustom];
        _img.backgroundColor = [UIColor lightGrayColor];
        [_img addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [_background addSubview:_img];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundColor = backColor;
    _background.backgroundColor = foreColor;
    _title.textColor = artiColor;
    self.background.frame = CGRectMake((int)12*ScaleX, (int)12*ScaleX, (int)350*ScaleX, (int)310*ScaleX);
    self.title.frame = CGRectMake((int)10*ScaleX, (int)210*ScaleX, (int)330*ScaleX, (int)20*ScaleX);
    self.desc.frame = CGRectMake((int)10*ScaleX, (int)240*ScaleX, (int)330*ScaleX, (int)15*ScaleX);
    self.details.frame = CGRectMake((int)10*ScaleX, (int)265*ScaleX, (int)(int)330*ScaleX, (int)45*ScaleX);
    self.player.frame = CGRectMake(0, 0, (int)350*ScaleX, (int)197*ScaleX);
    self.img.frame = CGRectMake(0, 0, (int)350*ScaleX, (int)197*ScaleX);
}

- (void)setMod:(ZGL24VideoModel *)mod{
    _mod = mod;
    self.height = (int)325*ScaleX;
    self.title.text = mod.title;
    self.desc.text = mod.desc;
    self.details.text = [NSString stringWithFormat:@"时长: %-2d:%-2d       播放次数: %d", (int)mod.length/60, (int)mod.length%60, (int)mod.playCount];
    [self.img sd_setImageWithURL:mod.cover forState:UIControlStateNormal];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    _player.backgroundColor = [UIColor blackColor];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"erorr videoCell webViewdidFailLoad");
}

- (void)click:(UIButton *)button{
    button.hidden = YES;
    //全局断点会崩溃
    NSString *htmlStr = [NSString stringWithFormat:@"\
                         <!DOCTYPE HTML>\
                         <html>\
                         <body bgcolor=\"#000000\">\
                         <video controls=\"controls\" width=\"%d\" height=\"%d\" autoplay=\"autoplay\" webkit-playsinline>\
                         <source src=\"%@\" type=\"video/mp4\" />\
                         该浏览器不支持\
                         </video> </body> </html>", (int)(335*ScaleX), (int)(175*ScaleX),_mod.mp4_url];
    [self.player loadHTMLString:htmlStr baseURL:nil];

}

- (void)dealloc{
    [_player stopLoading];
    _player.delegate = nil;
}
@end

